package com.som.service.excel.data2.process;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

import com.som.bean.data1.output.IpaddressJoin;
import com.som.bean.data2.input.CompDataInput;
import com.som.bean.data2.input.MergerCompDataInput;
import com.som.console.ExcelProcException;
import com.som.console.PathEnum;
import com.som.service.excel.ExcelConvDataProcessInf;
import com.som.service.excel.ExcelReadProcess;
import com.som.service.excel.data2.input.CompDataExcelRead;
import com.som.service.excel.data2.input.IpaddressJoinExcelRead;
import com.som.util.FileUtils;

/**
 * 将数据进行合并后的结果
 * 
 * @author liujun
 * @date 2016年4月10日
 * @verion 0.0.1
 */
public class Data2MergerProcess implements ExcelConvDataProcessInf<MergerCompDataInput>
{

	/**
	 * 地址输入处理信息
	 */
	private ExcelReadProcess<String, IpaddressJoin> ipAddrJoin = new IpaddressJoinExcelRead();

	/**
	 * excel多个数据源信息
	 */
	private ExcelReadProcess<String, CompDataInput> compDataProc = new CompDataExcelRead();

	public Map<String, MergerCompDataInput> convProcessData(Map<String, String> pathMap) throws ExcelProcException
	{
		// 响应结果数据
		Map<String, MergerCompDataInput> result = new LinkedHashMap<String, MergerCompDataInput>();

		// 读取输入文件1
		Map<String, IpaddressJoin> ipJoinMap = ipAddrJoin.readExcelDataMap(pathMap.get(PathEnum.DATA2_INPUT_NEAREAJOIN.getPathFlag()), 0, true);

		// 将文件1的数据放入
		if (null != ipJoinMap && !ipJoinMap.isEmpty())
		{
			Iterator<Entry<String, IpaddressJoin>> ipJoinIter = ipJoinMap.entrySet().iterator();

			MergerCompDataInput mergerData = null;
			Entry<String, IpaddressJoin> ipJoinEntry = null;
			while (ipJoinIter.hasNext())
			{
				ipJoinEntry = ipJoinIter.next();

				mergerData = new MergerCompDataInput();

				mergerData.setIpaddress(ipJoinEntry.getKey());
				mergerData.setNetwork(ipJoinEntry.getValue().getNetwork());
				mergerData.setNeInfo(ipJoinEntry.getValue().getNeInfo());
				mergerData.setAreainfo(ipJoinEntry.getValue().getAreainfo());

				result.put(ipJoinEntry.getKey(), mergerData);

			}
		}

		Iterator<Entry<String, String>> iterEntry = pathMap.entrySet().iterator();

		Entry<String, String> pathEntry = null;

		Map<String, CompDataInput> mapCompData = null;

		// 进行二层遍历的信息
		Iterator<Entry<String, CompDataInput>> compDataIter = null;
		Entry<String, CompDataInput> compEntry = null;

		MergerCompDataInput mergerInput = null;
		// 进行文件对应的数据填充
		LinkedHashMap<String, CompDataInput> compMap = null;

		String key = null;

		while (iterEntry.hasNext())
		{
			pathEntry = iterEntry.next();

			key = pathEntry.getKey();

			// 只有包括标识为多文件的才进行读取
			if (null != key && key.contains(PathEnum.DATA2_INPUT_COMPINPUT.getPathFlag()))
			{
				// 一个一个文件的读取数据
				mapCompData = compDataProc.readExcelDataMap(pathEntry.getValue(), 0, true);

				// 进行数据填充
				compDataIter = mapCompData.entrySet().iterator();

				while (compDataIter.hasNext())
				{
					compEntry = compDataIter.next();

					// 从总记录中取得以ip为key的数据进行填充
					mergerInput = result.get(compEntry.getKey());

					// 如果能找到此IP记录
					if (null != mergerInput)
					{
						// 进行数据填充
						compMap = mergerInput.getMargeMap();

						if (null == compMap)
						{
							compMap = new LinkedHashMap<String, CompDataInput>();
						}

						compMap.put(FileUtils.getFileName(pathEntry.getValue()), compEntry.getValue());

						mergerInput.setMargeMap(compMap);
					}
					result.put(compEntry.getKey(), mergerInput);
				}
			}

		}

		return result;
	}

}
